package com.dayatang.hrm.compensation.infra.hibernate;

import java.util.List;

import org.apache.tapestry5.hibernate.HibernateSessionManager;

import com.dayatang.commons.repository.EntityRepositoryHibernateTapestry;
import com.dayatang.hrm.compensation.domain.FiscalPeriod;
import com.dayatang.hrm.compensation.domain.FiscalPeriodRepository;
import com.dayatang.hrm.compensation.domain.FiscalYear;

@SuppressWarnings("unchecked")
public class FiscalPeriodRepositoryImpl extends
		EntityRepositoryHibernateTapestry<FiscalPeriod, Long> implements
		FiscalPeriodRepository {

	public FiscalPeriodRepositoryImpl(HibernateSessionManager sessionManager) {
		super(FiscalPeriod.class, sessionManager);
	}

	@Override
	public List<FiscalPeriod> findByFiscalYear(FiscalYear fiscalYear) {
		return getSessionManager().getSession().getNamedQuery(
				"FiscalPeriod.findByFiscalYear").setEntity("pYear", fiscalYear)
				.list();
	}

	@Override
	public FiscalPeriod getLatestPeriodByFiscalYear(FiscalYear fiscalYear) {

		List<FiscalPeriod> periods = findByFiscalYear(fiscalYear);

		if (periods.isEmpty()) {
			return null;
		} else {
			return periods.get(periods.size() - 1);
		}

	}
}
